//
//  THJGetMoreNewsController.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/9/23.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJGetMoreNewsController.h"
#import "THJSArticleCell.h"
#import <MJRefresh.h>
#import "THJWebviewVC.h"

@interface THJGetMoreNewsController ()
{
    NSInteger pageNo;
    NSInteger updateType;
}
@property(nonatomic,strong)NSMutableArray * newsArr;
@end

@implementation THJGetMoreNewsController
-(NSMutableArray*)newsArr{
    if (!_newsArr) {
        _newsArr = [[NSMutableArray alloc]init];
    }
    return _newsArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    pageNo = 1;
//    updateType = 1;
    self.title = @"铁合金现货网";
    [self setUpNav];
    [self.tableView registerNib:[UINib nibWithNibName:@"THJSArticleCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self getNewsWithType:self.urlType Page:@"1"];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downUpdate)];
    self.tableView.tableFooterView = [UIView new];
}
-(void)loadMore{
    updateType = 2;
    pageNo++;
    [self getNewsWithType:self.urlType Page:[NSString stringWithFormat:@"%ld",pageNo]];
}
-(void)downUpdate{
    updateType = 1;
    [self getNewsWithType:self.urlType Page:@"1"];
}
//左边返回按钮
- (void)setUpNav
{
    UIImage * img = [kGetImage(@"箭头")imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = backItem;
}
- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)getNewsWithType:(NSString*)type Page:(NSString*)page{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    __weak typeof(self) weakself = self;
    NSString * urlStr = [NSString stringWithFormat:@"%@bd_index.php?action=info&word=%@&page=%@&limit=10&token=dev",THJDomain,type,page];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [THJNetWork getNetDataFromUrl:urlStr callback:^(NSDictionary* receiveData, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        if([receiveData[@"state"] isEqual:@"success"]){
            if(updateType == 1){
                weakself.newsArr = receiveData[@"data"];
                [weakself.tableView reloadData];
            }else{
                for (NSDictionary * dic in receiveData[@"data"]) {
                    [weakself.newsArr addObject:dic];
                }
                [weakself.tableView reloadData];
            }
        }
    }];
}

#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.newsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    NSDictionary * dic = self.newsArr[indexPath.row];
    THJSArticleCell * cell3 = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell3.titleLab.text = dic[@"title"];
    cell3.timeLab.text = [THJTools timestampSwitchTime:[dic[@"addtime"] integerValue] andFormatter:@"YYYY-MM-dd"];
    return cell3;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.newsArr[indexPath.row];
    NSString *  urlstr = [NSString stringWithFormat:@"%@%@%@%@",THJDomain,@"sharepage.php?itemid=",dic[@"itemid"],@"&type=0&from=singlemessage&isappinstalled=1"];
    THJWebviewVC * vc = [[THJWebviewVC alloc] init];
    vc.detailUrl = urlstr;
    vc.title = @"铁合金现货网";
    NSLog(@"RRR--%@",vc.detailUrl);
    [self.navigationController pushViewController:vc animated:YES];
}
@end
